package com.hw.one.collect.dust.servcie;

import com.hw.one.collect.dust.dao.RaiseDustDao;
import com.hw.one.collect.dust.po.RaiseDust;
import com.hw.one.collect.dust.redis.BaseRedis;
import com.hw.one.collect.mapper.SiteDao;
import com.hw.one.collect.spadata.po.MissData;
import com.hw.one.collect.util.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/3.
 */
@Service
public class BaseInfoService {

    private static String siteNameStr = "sjwSiteNameWithCode";

    private static String lastDataStr = "sjwLastData";

    private static String isUseStr = "sjwIsUse";

    private static String sjwMissData = "sjwMissData";

    private static String sjwRegionCodeWithSiteCode = "sjwRegionCodeWithSiteCode";

    @Resource
    private BaseRedis baseRedis;

    @Resource
    private SiteDao siteDao;

    @Resource
    private RaiseDustDao raiseDustDao;

    /**
     * 根据站点名称，从redis中获取站点编码
     *
     *
     * @param siteName
     * @return
     */
    public List<String> getSiteCodeBySiteNameFromRedis(String siteName) {
        return baseRedis.getList(siteNameStr + siteName);
    }

    /**
     * 把站点名称插入到redis中。
     *
     * @param siteName
     * @param siteCode
     */
    public void insertSiteCodeBySiteNameFromRedis(String siteName, String siteCode) {
        List<String> resultList = getSiteCodeBySiteNameFromRedis(siteName);
        if (null == resultList || resultList.size() < 1) {
            baseRedis.addList(siteNameStr + siteName, siteCode);
        }else {
            if (!resultList.contains(siteCode)) {
                // 如果不包含信息则保存
                baseRedis.addList(siteNameStr + siteName, siteCode);
            }
        }
    }

    /**
     * 根据站点名称和站点编码把数据删除
     *
     * @param siteName
     * @param siteCode
     */
    public void deleteSiteCodeBySiteNameFromRedis(String siteName, String siteCode) {
        baseRedis.deleteList(siteNameStr, 0, siteCode);
    }

    /**
     * 根据站点名称获取最新redis中的数据
     *
     * @param siteCode
     * @return
     */
    public RaiseDust getLastDataBySiteCode(String siteCode) {
        Serializable serializable = baseRedis.getSer(lastDataStr+siteCode);
        Object obj = SerializeUtil.unSerialize((byte[]) serializable);
        if (obj instanceof RaiseDust) {
            return (RaiseDust) obj;
        }else {
            return null;
        }
    }

    /**
     * 获取最新redis中的缺失数据记录
     *
     * @param
     * @return
     */
    public List<MissData> getMissData() {
        List<MissData> missDatas = new ArrayList<>();
        List<Serializable> serList = baseRedis.getObjList(sjwMissData);
        for (Serializable ser : serList) {
            Object obj = SerializeUtil.unSerialize((byte[]) ser);
            if (obj instanceof MissData) {
                missDatas.add((MissData)obj);
            }
        }
        return missDatas;
    }


    /**
     * 删除缺数据记录
     *
     * @param ser
     */
    public void deleteMissDataByValue(Serializable ser) {
        baseRedis.deleteSerList(sjwMissData, 0, ser);
    }

    /**
     * 保存缺失数据记录
     *
     * @param ser
     */
    public void addMissData(Serializable ser) {
        baseRedis.addObjList(sjwMissData, ser);
    }

    public List<String> getUseSiteCodeList() {
        return baseRedis.getList(isUseStr);
    }

    public void addUseSiteCode(String siteCode) {
        baseRedis.addList(isUseStr, siteCode);
    }


    public String getRegionCodeBySiteCode(String siteCode) {
        return baseRedis.get(sjwRegionCodeWithSiteCode+siteCode);
    }

    public void insertRegionCodeWithSiteCode(String siteCode, String regionCode) {
        baseRedis.insert(sjwRegionCodeWithSiteCode+siteCode, regionCode);
    }


    /**
     * 保存最新的数据到redis key值为关键字+站点编码
     *
     * @param siteCode
     * @param raiseDust
     */
    public void insertLastData(String siteCode, RaiseDust raiseDust) {
        baseRedis.insertSer(lastDataStr + siteCode, SerializeUtil.ObjTOSerialize(raiseDust));
    }

}
